package com.junlt.p2p.app;

import android.content.Context;
import android.os.Build;
import android.os.StrictMode;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.view.Gravity;
import android.widget.Toast;

import com.google.gson.Gson;
import com.junlt.p2p.utils.SimplexToast;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;

/**
 * Created by tj on 2018/1/1.
 */

public class MyApplication extends MultiDexApplication {
    static Context _context;
    static Gson   _gson;
    private static RefWatcher mRefWatcher;
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        //继承MultiDexApplication 重写此方法，解决5.0以前版本分包找不到类问题
        MultiDex.install(this);
    }
    public static RefWatcher getRefWatcher() {
        return ((MyApplication) _context).mRefWatcher;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        _context = getApplicationContext();
        _gson=new Gson();
        //LeakCanary
        mRefWatcher = LeakCanary.install(this);
        //严格代码模式
        if (Constant.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                    .detectAll()
                    .penaltyFlashScreen()
                    .build());
            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                    .detectAll()
                    .penaltyLog()
                    .build());
        }
    }
    public static synchronized MyApplication context() {
        return (MyApplication) _context;
    }
    public static synchronized Gson gson() {
        return _gson;
    }
    public static void showToastShort(String message) {
        showToast(message, Toast.LENGTH_SHORT, 0, Gravity.BOTTOM);
    }

    public static void showToast(String message, int duration, int icon, int gravity) {
        Context context = _context;
        if (context != null)
            SimplexToast.show(context, message, gravity, duration);
    }
}
